/*Confidential and Proprietary, Subject to the Non-Disclosure Agreement,
 Version 1.0, between the Development Gateway Foundation, Inc and the
 Recipient Copyright 2001-2004 Development Gateway Foundation, Inc.
 */
package ro.gateway.aida.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ro.gateway.aida.obj.OrgType;
import ro.gateway.aida.obj.db.OrgTypeDB;
import ro.gateway.aida.struts.MultiTilesAction;
import ro.gateway.aida.usr.Arbiter;
import ro.gateway.aida.usr.Perms;
import ro.gateway.aida.usr.User;
import ro.gateway.aida.usr.UserUtils;
import ro.xblue.translator.TranslatorUtils;

/**
 * @author mihai e-mail: mihai@ro-gateway.org
 */
public class EditOrgTypeAction extends MultiTilesAction {

	public ActionForward modePrepare(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Arbiter ar = Arbiter.getManager(token);
		User user = UserUtils.getLogged(request, response);
		HttpSession session = request.getSession();
		if (user == null || !ar.canDo(request, Perms.ADMIN_TOOLS)) {
			session.invalidate();
			return mapping.findForward("norights");
		}

		return modeSelect(mapping, form, request, response);
	}

	public ActionForward modeList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		OrgTypeDB otdb = OrgTypeDB.getManager(token);
		String lang = TranslatorUtils.getCurrentLanguage(request, sc)
				.getLanguage();
		List l = otdb.getAllLocalized(lang);
		request.setAttribute(OrgType.LIST, l);
		return mapping.findForward("list");
	}

	public ActionForward modeEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		int id = Integer.parseInt(request.getParameter("id"));
		OrgTypeDB otdb = OrgTypeDB.getManager(token);
		String lang = TranslatorUtils.getCurrentLanguage(request, sc)
				.getLanguage();

		OrgType t = otdb.getById(id);
		t.getName().setViewLang(lang);
		request.setAttribute("item", t);
		return mapping.findForward("edit");
	}

	public ActionForward modeNew(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		OrgTypeDB otdb = OrgTypeDB.getManager(token);
		String lang = TranslatorUtils.getCurrentLanguage(request, sc)
				.getLanguage();

		OrgType t = new OrgType();
		t.setViewLang(lang);
		t.setId(0);
		request.setAttribute("item", t);
		return mapping.findForward("edit");
	}

	public ActionForward modeSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String sid = request.getParameter("id");
		int id;
		if (sid == null || sid.equals(""))
			id = 0;
		else
			id = Integer.parseInt(sid);
		OrgTypeDB otdb = OrgTypeDB.getManager(token);
		String lang = TranslatorUtils.getCurrentLanguage(request, sc)
				.getLanguage();
		String text = request.getParameter("name");

		otdb.insert(id, lang, text);

		if (id == 0)
			return modeList(mapping, form, request, response);
		return modeEdit(mapping, form, request, response);
	}

	public ActionForward modeDelete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String[] delcats = request.getParameterValues("delcats");
		OrgTypeDB otdb = OrgTypeDB.getManager(token);
		boolean b = true;
		for (int i = 0; i < delcats.length && b; i++)
			b = otdb.delete(Integer.parseInt(delcats[i]));
		if (!b)
			return mapping.findForward("cannotdel");
		return modeList(mapping, form, request, response);
	}

	public ActionForward modeSelect(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		if (request.getParameter("delete") != null)
			return modeDelete(mapping, form, request, response);

		if (request.getParameter("save") != null)
			return modeSave(mapping, form, request, response);
		if (request.getParameter("new") != null)
			return modeNew(mapping, form, request, response);

		if (request.getParameter("edit") != null)
			return modeEdit(mapping, form, request, response);

		return modeList(mapping, form, request, response);

	}

}